import { logout, getInfo } from '@/api/login'
import { getToken, removeToken } from '@/utils/auth'
import { Message } from "element-ui";

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    menus: [],
    buttons:[],
    columns:[],
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_BUTTONS: (state, buttons) => {
      state.buttons = buttons
    },
    SET_COLUMNS: (state, columns) => {
      state.columns = columns
    }
  },

  actions: {
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        const token = getToken()
        if (token) {
          getInfo().then(response => {
            const data = response.data
            if (data.userMenu && data.userMenu.length > 0) { // 验证返回的roles是否是一个非空数组
              commit('SET_ROLES', data.userMenu)
            } else {
              reject('无权限')
            }
            commit('SET_MENUS', data.roleMenu)
            commit('SET_BUTTONS', data.roleButton)
            commit('SET_COLUMNS', data.roleColumn)
            commit('SET_NAME', data.userName)
            commit('SET_AVATAR', data.headImg)
            resolve(response)
          }).catch(error => {
            Message({
              message: "未登录或登陆已失效，请重新登陆",
              type: "error",
              duration: 5 * 1000
            });
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            commit('SET_MENUS', [])
            commit('SET_BUTTONS', [])
            commit('SET_COLUMNS', [])
            commit('SET_NAME', '')
            commit('SET_AVATAR', '')
            removeToken()
            reject(error)
          })
        } else {
          reject()
        }
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_MENUS', [])
        commit('SET_BUTTONS', [])
        commit('SET_NAME', '')
        removeToken()
        resolve()
        // const token = getToken()
        // logout(token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   commit('SET_MENUS', [])
        //   commit('SET_BUTTONS', [])
        //   commit('SET_NAME', '')
        //   removeToken()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    ClearRoles({ commit }) {
      return new Promise(resolve => {
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      })
    }
  }
}

export default user
